//
//  HZZFViewController.m
//  HZZFAdSDK
//
//  Copyright (c) 2025 早范. All rights reserved.
//

#import "HZZFViewController.h"
#import <HZZFAdSDK/HZZFAdSDKNativeAd.h>
#import <HZZFAdSDK/HZZFAdSDKNativeExpressAd.h>
#import <HZZFAdSDK/HZZFAdSDKInterstitialAd.h>
#import <HZZFAdSDK/HZZFAdSDKBannerAdView.h>
#import <HZZFAdSDK/HZZFAdSDKServiceAdView.h>
#import <HZZFAdSDK/HZZFAdSDKRewardVideoAd.h>
#import <HZZFAdSDK/HZZFAdapterNativeExpressCallBack.h>
#import <HZZFAdSDK/HZZFAdSDK.h>
#import "BannerViewController.h"
#import "InterstitialViewController.h"
#import "NativeAdViewController.h"
#import "ServiceViewController.h"
#import "NativeExpressAdViewController.h"

@interface HZZFViewController ()<HZZFSDKServiceAdViewDelegate,HZZFAdSDKRewardVideoAdDelegate,HZZFSDKBannerAdViewDelegate,HZZFAdSDKInterstitialAdDelegate,HZZFAdSDKNativeExpressAdDelegate,HZZFAdSDKNativeAdDelegate, UITableViewDelegate,UITableViewDataSource>

@property(nonatomic, strong) HZZFAdSDKNativeExpressAd *nativeExpressAd;

@property (nonatomic, strong) HZZFAdSDKInterstitialAd *interstitialAd;

@property (nonatomic, strong) HZZFAdSDKBannerAdView *bannerView;

@property (nonatomic, strong) HZZFAdSDKRewardVideoAd *rewardVideoAd;

@property (nonatomic, strong) HZZFAdSDKServiceAdView *serviceAdView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation HZZFViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = [NSString stringWithFormat:@"HZZFAdSDK demo(%@)", [HZZFAdSDK getSDKVersion]];
    // Do any additional setup after loading the view, typically from a nib.
    self.dataSource = @[@"插屏",@"banner",@"非标",@"信息流模版",@"自渲染"];

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.tableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    UIView *view = [cell.contentView viewWithTag:999];
    if (view) {
        [view removeFromSuperview];
    }
    UILabel *labTitle = [[UILabel alloc]init];
    labTitle.font = [UIFont systemFontOfSize:20];
    labTitle.textColor = [UIColor blackColor];
    labTitle.tag = 999;
    labTitle.text = [self.dataSource objectAtIndex:indexPath.row];
    [cell.contentView addSubview:labTitle];
    labTitle.frame = CGRectMake(19, 15, [UIScreen mainScreen].bounds.size.width - 38, 50);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    NSString *name = self.dataSource[indexPath.row];
    if([name isEqualToString:@"插屏"]){
        InterstitialViewController *test = [[InterstitialViewController alloc] init];
        [self.navigationController pushViewController:test animated:YES];
        
    }else if([name isEqualToString:@"非标"]){
        ServiceViewController *test = [[ServiceViewController alloc] init];
        [self.navigationController pushViewController:test animated:YES];
        
    }else if([name isEqualToString:@"banner"]){
        BannerViewController *test = [[BannerViewController alloc] init];
        [self.navigationController pushViewController:test animated:YES];
        
    }else if([name isEqualToString:@"自渲染"]){
        NativeAdViewController *test = [[NativeAdViewController alloc] init];
        [self.navigationController pushViewController:test animated:YES];
        
    }else if([name isEqualToString:@"信息流模版"]){
        NativeExpressAdViewController *test = [[NativeExpressAdViewController alloc] init];
        [self.navigationController pushViewController:test animated:YES];
        
    }
}

#pragma mark - 服务广告
- (void)loadServiceAdView{
    self.serviceAdView = [[HZZFAdSDKServiceAdView alloc]initWithFrame:CGRectMake(0, 200, 50, 50)];
    self.serviceAdView.delegate = self;
    self.serviceAdView.controller = self;
    self.serviceAdView.posId = @"";
    // 设置展示关闭按钮
    self.serviceAdView.showCloseView = YES;
    [self.serviceAdView loadAndShow];
}

#pragma mark - HZZFAdSDKServiceAdViewDelegate
/**
 广告获取成功
 
 @param serviceAdView banner实例
 */
- (void)HZZF_serviceAdViewDidReceived:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

/**
 广告拉取失败
 
 @param serviceAdView banner实例
 @param error 错误描述
 */
- (void)HZZF_serviceAdViewFailToReceived:(HZZFAdSDKServiceAdView *)serviceAdView error:(NSError *)error{
    NSLog(@"%s", __func__);
}

/**
 广告点击
 
 @param serviceAdView 广告实例
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)HZZF_serviceAdViewClicked:(HZZFAdSDKServiceAdView *)serviceAdView loadingPageURL:(NSString *)loadingPageURL{
    NSLog(@"%s", __func__);
}

/**
 广告关闭
 
 @param serviceAdView 广告实例
 */
- (void)HZZF_serviceAdViewClose:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

/**
 广告展示
 
 @param serviceAdView 广告实例
 */
- (void)HZZF_serviceAdViewExposure:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

/**
 广告展示上报
 
 @param serviceAdView 广告实例
 */
- (void)HZZF_serviceAdViewExposureReport:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

/**
 广告点击上报
 
 @param serviceAdView 广告实例
 */
- (void)HZZF_serviceAdViewClickedReport:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

/**
 关闭落地页
 
 @param serviceAdView 广告实例
 */
- (void)HZZF_serviceAdViewCloseLandingPage:(HZZFAdSDKServiceAdView *)serviceAdView{
    NSLog(@"%s", __func__);
}

#pragma mark - 激励视频广告
- (void)loadRewardVideo{
    self.rewardVideoAd = [[HZZFAdSDKRewardVideoAd alloc]init];
    self.rewardVideoAd.delegate = self;
    self.rewardVideoAd.controller = self;
    self.rewardVideoAd.posId = @"23636003";
    self.rewardVideoAd.videoMuted = YES;
    [self.rewardVideoAd loadAdData];
}

#pragma mark - HZZFAdSDKRewardVideoAdDelegate
/**
 广告数据加载成功回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidLoad:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
    // 价格上报
//    NSInteger price = [self.rewardVideoAd getBidPrice];
//    [self.rewardVideoAd sendWinNotice:price];
    [self.rewardVideoAd showRewardVideoAdFromRootViewController:self];
}

/**
 视频播放页即将展示回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdWillVisible:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告曝光回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidExposed:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告曝光上报回调（只会回调一次）

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidExposeReport:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频播放页关闭回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidClose:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告信息点击回调（可能回调多次）

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidClicked:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告信息点击上报回调（只会回调一次）

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidClickReport:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告各种错误信息回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 @param error 具体错误信息
 */
- (void)HZZF_rewardVideoAd:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd didFailWithError:(NSError *)error{
    NSLog(@"%s", __func__);
}


/**
 视频广告播放达到激励条件回调

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidRewardEffective:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

/**
 视频广告视频播放完成

 @param rewardedVideoAd HZZFAdSDKRewardVideoAd 实例
 */
- (void)HZZF_rewardVideoAdDidPlayFinish:(HZZFAdSDKRewardVideoAd *)rewardedVideoAd{
    NSLog(@"%s", __func__);
}

#pragma mark - banner广告
- (void)loadBanner{
    CGFloat width = self.view.frame.size.width;
    CGFloat height = width * 5 / 32;
    self.bannerView = [[HZZFAdSDKBannerAdView alloc]initWithFrame:CGRectMake(0, 200, width, height)];
    self.bannerView.delegate = self;
    self.bannerView.controller = self;
    self.bannerView.posId = @"";
    // 设置展示关闭按钮
    self.bannerView.showCloseView = YES;
    [self.bannerView loadAndShow];
}

#pragma mark - HZZFAdSDKBannerAdViewDelegate
/**
 广告获取成功
 
 @param bannerAdView banner实例
 */
- (void)HZZF_bannerViewDidReceived:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
    // 价格上报
//    NSInteger price = [self.bannerView getBidPrice];
//    [self.bannerView sendWinNotice:price];
}

/**
 广告拉取失败
 
 @param bannerAdView banner实例
 @param error 错误描述
 */
- (void)HZZF_bannerViewFailToReceived:(HZZFAdSDKBannerAdView *)bannerAdView error:(NSError *)error{
    NSLog(@"%s", __func__);
}

/**
 广告关闭
 
 @param bannerAdView 广告实例
 */
- (void)HZZF_bannerViewClose:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
}

/**
 广告展示
 
 @param bannerAdView 广告实例
 */
- (void)HZZF_bannerViewExposure:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
}

/**
 广告展示上报
 
 @param bannerAdView 广告实例
 */
- (void)HZZF_bannerViewExposureReport:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
}

/**
 广告点击上报
 
 @param bannerAdView 广告实例
 */
- (void)HZZF_bannerViewClickedReport:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
}

/**
 关闭落地页
 
 @param bannerAdView 广告实例
 */
- (void)HZZF_bannerAdCloseLandingPage:(HZZFAdSDKBannerAdView *)bannerAdView{
    NSLog(@"%s", __func__);
}

- (void)HZZF_bannerViewClicked:(nonnull HZZFAdSDKBannerAdView *)bannerView loadingPageURL:(nonnull NSString *)loadingPageURL {
    NSLog(@"%s", __func__);
}


#pragma mark - 插屏广告
- (void)loadInterstitialAd {
    self.interstitialAd = [[HZZFAdSDKInterstitialAd alloc]init];
    self.interstitialAd.delegate = self;
    self.interstitialAd.controller = self;
    self.interstitialAd.posId = @"13945365";
    [self.interstitialAd loadAdData];
}

#pragma mark - HZZFAdSDKInterstitialAdDelegate
/**
 HZZFAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
 */
- (void)HZZF_interstitialAdSuccedToLoad:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
    // 获取价格，价格上报
    //    NSInteger price = [self.interstitialAd getBidPrice];
    //    [self.interstitialAd sendWinNotice:price];
    // 设置控制器，展示插屏广告
    self.interstitialAd.controller = self;
    [self.interstitialAd show];
}

/**
 HZZFAdSDKIntertitialAd请求失败回调
 
 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
 */
- (void)HZZF_interstitialAdFailedToLoad:(HZZFAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd展示在屏幕内回调
 
 @param interstitialAd 插屏广告实例对象
 */
- (void)HZZF_interstitialAdDidPresent:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd展示在屏幕内失败回调
 
 @param interstitialAd 插屏广告实例对象
 */
- (void)HZZF_interstitialAdFailedToPresent:(HZZFAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd点击回调
 
 @param interstitialAd 插屏广告实例对象
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
 */
- (void)HZZF_interstitialAdDidClick:(HZZFAdSDKInterstitialAd *)interstitialAd loadingPageURL:(NSString *)loadingPageURL{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd关闭回调
 
 @param interstitialAd 插屏广告实例对象
 */
- (void)HZZF_interstitialAdDidClose:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)HZZF_interstitialAdExposure:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd展示上报回调
 
 @param interstitialAd 广告实例
 */
- (void)HZZF_interstitialAdExposureReport:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}


/**
 HZZFAdSDKIntertitialAd点击上报回调
 
 @param interstitialAd 插屏广告实例对象
 */
- (void)HZZF_interstitialAdDidClickReport:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}

/**
 HZZFAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)HZZF_interstitialAdCloseLandingPage:(HZZFAdSDKInterstitialAd *)interstitialAd{
    NSLog(@"%s", __func__);
}


#pragma mark - 信息流模板广告
- (void)loadNativeExpress {
    CGFloat width = self.view.frame.size.width;
    self.nativeExpressAd = [[HZZFAdSDKNativeExpressAd alloc]initWithAdSize:CGSizeMake(width, 50)];
    self.nativeExpressAd.posId = @"99908725";
    self.nativeExpressAd.delegate = self;
    self.nativeExpressAd.expressType = HZZFAdSDKNativeExpressTypeImage;
    self.nativeExpressAd.controller = self;
    [self.nativeExpressAd load:1];
}

#pragma mark - HZZFAdSDKNativeExpressAdDelegate
/**
 信息流模版广告请求成功
 
 @param nativeExpressAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)HZZF_nativeExpressAdSucessToLoad:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                             adViewArray:(NSArray<__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *> *)adViewArray{
    NSLog(@"%s", __func__);
    UIView<HZZFAdapterNativeExpressAdViewDelegate> *adView = [adViewArray firstObject];
    [adView render];
    [self.view addSubview:adView];
    CGSize adViewSize = adView.frame.size;
    adView.frame = CGRectMake(0, 200, adViewSize.width, adViewSize.height);
}

/**
 信息流模版广告请求失败
 
 @param nativeExpressAd 广告实例
 @param error 请求错误描述
 */
- (void)HZZF_nativeExpressAdFailToLoad:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                                 error:(NSError *)error{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告渲染成功
 
 @param adView 广告视图
 */
- (void)HZZF_nativeExpressAdViewRenderOrRegistSuccess:(UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告渲染失败
 
 @param adView 广告视图
 */
- (void)HZZF_nativeExpressAdViewRenderOrRegistFail:(UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告被点击
 
 @param nativeExpressAd 广告模板
 */
- (void)HZZF_nativeExpressAdClicked:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                             adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告被关闭
 
 @param nativeExpressAd 广告模板
 */
- (void)HZZF_nativeExpressAdClose:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                           adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告被展示
 
 @param nativeExpressAd 广告实例
 */
- (void)HZZF_nativeExpressAdExposure:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                              adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告展示上报
 
 @param nativeExpressAd 广告实例
 */
- (void)HZZF_nativeExpressAdExposureReport:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                                    adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告点击上报
 
 @param nativeExpressAd 广告模板
 */
- (void)HZZF_nativeExpressAdClickedReport:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                                   adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

/**
 信息流模版广告落地页被关闭
 
 @param nativeExpressAd 广告实例
 */
- (void)HZZF_nativeExpressAdCloseLandingPage:(HZZFAdSDKNativeExpressAd *)nativeExpressAd
                                      adView:(__kindof UIView<HZZFAdapterNativeExpressAdViewDelegate> *)adView{
    NSLog(@"%s", __func__);
}

@end

